using UnityEngine;
using System.Collections.Generic;
//有这行生成的脚本都是UTF-8形式的

namespace MyGame
{
    public enum ChessType
    {
        none = 0,
        white = 1,
        black = 2,
        select = 3,
    }

    public class ChessManager : BaseManager<ChessManager>
    {
        public Sprite[] chessSprites;
        public Chess[,] chesses = new Chess[15,15];

        private bool isInitChesses = false;

        private int blackNum;
        private int whiteNum;

        private Stack<Chess> blackMoveHistory = new Stack<Chess>();
        private Stack<Chess> whiteMoveHistory = new Stack<Chess>();

        private void Awake()
        {
           
        }

        private void Start()
        {
            
        }

        public void Init()
        {
            InitData();
            InitMapSprite();
        }

        void InitData()
        {
            blackNum = 0;
            whiteNum = 0;
            blackMoveHistory.Clear();
            whiteMoveHistory.Clear();
        }

        public void InitMapSprite()
        {
            if (!isInitChesses)
            {
                int index = 0;
                Transform objTransform = UIManager.Instance.GetGameObject(UIType.BoardUI).transform;
                var chessLocal = objTransform.GetComponentsInChildren<Chess>();
                for (int i = 0; i < 15; i++)
                {
                    for (int j = 0; j < 15; j++)
                    {
                        chesses[i, j] = chessLocal[index];
                        index++;
                    }
                }

                isInitChesses = true;
            }       

            foreach (var item in chesses)
            {
                item.Init(GetSprite(ChessType.none));
            }
        }

        public void AddBlackNumber(Chess chess)
        {
            blackNum++;
            blackMoveHistory.Push(chess);
            BackGroundUI.Instance.UpdateTextBlack(blackNum);
        }

        public void AddWhiteNumber(Chess chess)
        {
            whiteNum++;
            whiteMoveHistory.Push(chess);
            BackGroundUI.Instance.UpdateTextWhite(whiteNum);
        }

        public void LoseNumber()
        {
            if (blackMoveHistory.Count > 0 && whiteMoveHistory.Count > 0)
            {
                LoseBlackNumber();
                LoseWhi1teNumber();
            }  
        }

        public void LoseBlackNumber()
        {
            blackNum--;
            var chessPop = blackMoveHistory.Pop();
            MapManager.Instance.UpdateMap(chessPop.row,chessPop.arr);
            chessPop.Init(GetSprite(ChessType.none));
            BackGroundUI.Instance.UpdateTextBlack(blackNum);
            Debug.Log("黑棋数量:" + blackMoveHistory.Count);
        }

        public void LoseWhi1teNumber()
        {
            whiteNum--;
            var chessPop = whiteMoveHistory.Pop();
            MapManager.Instance.UpdateMap(chessPop.row, chessPop.arr);
            chessPop.Init(GetSprite(ChessType.none));
            BackGroundUI.Instance.UpdateTextWhite(whiteNum);
            Debug.Log("白棋数量:" + blackMoveHistory.Count);
        }

        public Sprite GetSprite(ChessType chessType)
        {
            return chessSprites[(int)chessType];
        }

        public void AIPlayChess(int row,int arr)
        {

        }
    }
}
